package com.curluin.inputInterpretation;

import com.curluin.controll.Controller;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class TiltInterpreter implements SensorEventListener{

	private static final String LOG_TAG="TiltInterpretor";

	private Controller controller;
	private SensorManager sensorManager;
	private Sensor accelerationSensor;
//	private TiltEvent previousEvent;

	
	public TiltInterpreter(Controller controller){
		this.controller=controller;

		sensorManager=(SensorManager)((Activity)controller).getSystemService(Context.SENSOR_SERVICE);
		accelerationSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		sensorManager.registerListener(this, accelerationSensor,SensorManager.SENSOR_DELAY_GAME);
		//		Log.i(LOG_TAG, "created");
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//			Log.i(LOG_TAG,"Y "+event.values[1]*2*360/Math.PI);
			TiltEvent e = new TiltEvent(event);
			if(controller!=null)controller.handleTiltEvent(e);
//			previousEvent=e;
		}
	}

	public void destroy() {
		sensorManager.unregisterListener(this);
		sensorManager=null;
		accelerationSensor=null;
		controller=null;
	}
	

	
	
}
